<template>
  <div class="cus-login-frame">
    <video src="../../assets/loginBg.webm" preload="auto" muted autoplay loop></video>
    <div class="login-form">
      <h1>电商科技·运营管理平台</h1>
      <p>
        <el-input placeholder="账号" autocomplete="off" prefix-icon="el-icon-user" v-model="form.username"></el-input>
      </p>
      <p>
        <el-input
            placeholder="密码"
            autocomplete="new-password"
            prefix-icon="el-icon-lock"
            show-password
            type="password"
            v-model="form.password"></el-input>
      </p>
      <p>
        <el-row>
          <el-col :span="18">
            <el-input placeholder="验证码" autocomplete="off" prefix-icon="el-icon-user" v-model="form.validCode"></el-input>
          </el-col>
          <el-col :span="6"><img @click="test" height="30px" width="90px" src="http://localhost:8081/admin/validCode"></el-col>
        </el-row>
      </p>
      <el-button :loading="loading" @click="loginFn" type="primary" class="login-btn" round>登 录</el-button>
    </div>
  </div>
</template>

<script>
import {reactive, toRefs, getCurrentInstance} from "vue";
import {ElMessage} from "element-plus";
import router from "@/router";

export default {
  name: 'LoginView',
  components: {},
  setup() {
    const axios = getCurrentInstance().appContext.config.globalProperties.$axios;
    const state = reactive({
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      search: "",
      form: {
        username: 'admin',
        password: '123456',
        validCode: ''
      },
      dialogVisible: false,
      loading: false
    })
    const test = () => {
      console.log('test')
    }
    const loginFn = async () => {
      if (state.form.username === '' || state.form.username === undefined) {
        ElMessage.warning('账号不能为空')
        return;
      }
      if (state.form.password === '' || state.form.password === undefined) {
        ElMessage.warning('密码不能为空')
        return;
      }
      if (state.form.validCode === '' || state.form.validCode === undefined) {
        ElMessage.warning('验证码不能为空')
        return;
      }
      // 加载登录动画
      state.loading = true
      let res = await axios.post('/admin/login', state.form);
      // 关闭登录动画
      state.loading = false
      // 设置token值
      localStorage.setItem('token', res.data.data);
      // 判断返回结果集
      if (res.data.code === "1") {
        ElMessage.success('登陆成功')
        await router.push("/index");
      } else {
        ElMessage.error(res.data.msg)
      }
    }
    return {
      ...toRefs(state),
      loginFn,
      test
    }
  }
}
</script>
<style scoped lang="scss">
.cus-login-frame {
  display: flex;
  height: 100vh;
  width: 100vw;
  // background: url('') center / 100% no-repeat;
  video {
    position: absolute;
    pointer-events: none;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

body {
  height: 100vh;
  background-color: #fbfbfd;
}

.login-form {
  h1 {
    text-align: center;
    padding: 30px 0;
    color: #fff;
    text-shadow: 0px 1px 2px #333;
  }

  backdrop-filter: blur(50px);
  background-color: rgba(160, 160, 160, 0.2);
  width: 500px;
  border-radius: 34px;
  box-shadow: 0 11px 34px 0 rgba(0, 0, 0, 0.3);
  padding: 40px;
  margin: 180px auto auto;
  font-size: 14px;
  transition: all 0.5s;

  p {
    margin: 20px 30px;

    .el-input__inner {
      height: 44px;
      line-height: 44px;
    }

    input {
      font-size: 15px;
      background-color: #fff;
      border-radius: 100px;
    }

    span {
      line-height: 30px;
    }
  }

  .login-btn {
    width: 140px;
    display: block;
    font-size: 15px;
    margin: 60px auto 0;
    box-shadow: 0 11px 34px 0 rgba(0, 0, 0, 0.3);
  }
}
</style>
